//
//  LTMessageImageBody.m
//  LTIM
//
//  Created by 俊松何 on 2017/4/11.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTMessageImageBody.h"

static const NSString *orginimagePath = @"chatOrginImagebuffer";

static const NSString *thumbnailimagePath = @"chatthumbnailImagebuffer";

static const NSString *thumbnailPath = @"?x-oss-process=image/resize,w_200";

@interface LTMessageImageBody()


@end

@implementation LTMessageImageBody

- (instancetype)initWithImage:(UIImage *)image
                  displayName:(NSString *)aDisplayName
{
        if(self = [super init])
        {

          NSData *orginData = UIImageJPEGRepresentation(image, 0.1);
          NSData *thumbData = UIImageJPEGRepresentation(image, 0.05);
            _imageW = image.size.width;
            _imageH = image.size.height;
            
          [self saveOrginImageData:orginData];
          [self saveThumbnailimageData:thumbData];

            
        }
    return self;
}

-(NSString *)thumbnailRemotePath
{

    return [NSString stringWithFormat:@"%@%@",_orginRemotePath,thumbnailPath];

}

-(NSString *)orginLocalPath
{

    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    _orginLocalPath = [NSString stringWithFormat:@"%@/%@",documentPath,_orginLocalrelativePath];
    
    return _orginLocalPath;

}

-(NSString *)thumbnailLocalPath
{
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    _thumbnailLocalPath = [NSString stringWithFormat:@"%@/%@",documentPath,_thumbnailLocalrelativePath];
    
    return _thumbnailLocalPath;


}

-(void)saveThumbnailimageData:(NSData *)data
{
    
    int random = arc4random() % 1000;
    
    
    NSString *filename = [NSString stringWithFormat:@"thumbnail%d%ld.jpg",random, (long)[[NSDate date] timeIntervalSince1970] * 1000];
    
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    
    documentPath = [NSString stringWithFormat:@"%@/%@",documentPath,thumbnailimagePath];
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:documentPath])
    {
        [fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *failePath = [NSString stringWithFormat:@"%@/%@",documentPath,filename];
    _thumbnailLocalrelativePath = [NSString stringWithFormat:@"%@/%@",thumbnailimagePath,filename];
     [fileManager createFileAtPath:failePath contents:data attributes:nil];

    
    
}

-(void)saveOrginImageData:(NSData *)data
{
    int random = arc4random() % 1000;
    
    NSString *filename = [NSString stringWithFormat:@"orgin%d%ld.jpg",random, (NSInteger)[[NSDate date] timeIntervalSince1970] * 1000];
    
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    
    documentPath = [NSString stringWithFormat:@"%@/%@",documentPath,orginimagePath];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:documentPath])
    {
        [fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *failePath = [NSString stringWithFormat:@"%@/%@",documentPath,filename];
    _orginLocalrelativePath = [NSString stringWithFormat:@"%@/%@",orginimagePath,filename];
    [fileManager createFileAtPath:failePath contents:data attributes:nil];

}



@end
